package com.baoli.blzj.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.baoli.blzj.bean.home.HomeMenuBean;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.baoli.blzj.Constants;
import com.baoli.blzj.MainActivity;
import com.baoli.blzj.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * 首页功能按钮适配器 by John
 */
public class HomeRollMeunAdapter extends StaticPagerAdapter implements Constants {

    private List<List<HomeMenuBean>> totalList;
    private Context context;
    private LayoutInflater inflater;
    private GridView pager_photos;
    private View view;
    private HomeMenuGridViewAdapter menuAdapter;
    private String phone = "";
    public SharedPreferences spn;
    public SharedPreferences.Editor editor;

    public HomeRollMeunAdapter(Context context) {
        this.context = context;
        this.totalList = new ArrayList<>();
        this.inflater = (LayoutInflater) LayoutInflater.from(context).getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        spn = context.getSharedPreferences(SPN_BLZJ, MODE_PRIVATE);
        editor = spn.edit();
    }

    public void setList(List<List<HomeMenuBean>> hBean) {
        totalList.clear();
        append(hBean);
    }

    public void append(List<List<HomeMenuBean>> hBean) {
        totalList.addAll(hBean);
        notifyDataSetChanged();
    }

    @Override
    public View getView(ViewGroup container, final int position) {
        //加载网络图片
        container = (ViewGroup) inflater.inflate(R.layout.view_pager_gn_menu, null);
        pager_photos = (GridView) container.findViewById(R.id.pager_photos);
        menuAdapter = new HomeMenuGridViewAdapter(context, totalList.get(position));
        pager_photos.setAdapter(menuAdapter);
        menuAdapter.notifyDataSetChanged();
        view = container.findViewById(R.id.line);
        pager_photos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                //setOnClicks(totalList.get(position), i);
            }
        });
        if (totalList.size() <= 1){
            view.setVisibility(View.GONE);
        }else {
            view.setVisibility(View.VISIBLE);
        }
        return container;
    }

    @Override
    public int getCount() {
        return totalList.size();
    }

    /*public void setOnClicks(List<HomeMenuBean> menuList, int position) {

        if ("1".equals(menuList.get(position).getFuc_type())) {
            if (menuList.get(position).getFuc_url().length() > 0) {
                Intent intent = new Intent();
                intent.setClass(context, PublicHaveTitleWebActivity.class);
                intent.putExtra("url", menuList.get(position).getFuc_url());
                intent.putExtra("name", menuList.get(position).getFun_name());
                context.startActivity(intent);
            }
            if ("1".equals(menuList.get(position).getFuncType())) {
                if ("wyjf".equals(menuList.get(position).getFuc_classname())) {

                    if (spn.getBoolean(SPN_IS_LOGIN, false)) {
                        if (spn.getString(SPN_SQID, "").equals("")) {
                            CustomToast.showToast(context, "请选择小区", 1000);
                        } else {
                            Intent intent = new Intent();
                            intent.setClass(context, HomeWyfwjfActivity.class);
                            context.startActivity(intent);
                        }
                    } else {
                        Intent intent = new Intent();
                        intent.setClass(context, PersonalLoginActivity.class);
                        ((MainActivity) context).startActivityForResult(intent, 101);
                    }
                } else if ("wykf".equals(menuList.get(position).getFuc_classname())) {
                    httpGetKeFu(HTTP_ONLINE_SERVICE);
                } else if ("jshs".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomeBodyBuildingActivity.class);
                    context.startActivity(intent);

                } else if ("dcxs".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent(context, HomeLandListActivity.class);
                    intent.putExtra("name", menuList.get(position).getFun_name());
                    context.startActivity(intent);
                } else if ("wybx".equals(menuList.get(position).getFuc_classname())) {
                    if (spn.getBoolean(SPN_IS_LOGIN, false)) {
                        if (spn.getString(SPN_SQID, "").equals("")) {
                            CustomToast.showToast(context, "请选择小区", 1000);
                        } else {
                            Intent intent = new Intent();
                            intent.setClass(context, HomeRepairOrderListActivity.class);
                            context.startActivity(intent);
                        }
                    } else {
                        Intent intent = new Intent();
                        intent.setClass(context, PersonalLoginActivity.class);
                        ((MainActivity) context).startActivityForResult(intent, 101);
                    }

                } else if ("wytz".equals(menuList.get(position).getFuc_classname())) {
                    if (spn.getBoolean(SPN_IS_LOGIN, false)) {
                        if (spn.getString(SPN_SQID, "").equals("")) {
                            CustomToast.showToast(context, "请选择小区", 1000);
                        } else {
                            Intent intent = new Intent();
                            intent.setClass(context, HomePropertyNoticeActivity.class);
                            context.startActivity(intent);
                        }
                    } else {
                        Intent intent = new Intent();
                        intent.setClass(context, PersonalLoginActivity.class);
                        ((MainActivity) context).startActivityForResult(intent, 101);
                    }
                } else if ("sxts".equals(menuList.get(position).getFuc_classname())) {
                    if (spn.getBoolean(SPN_IS_LOGIN, false)) {
                        if (spn.getString(SPN_SQID, "").equals("")) {
                            CustomToast.showToast(context, "请选择小区", 1000);
                        } else {
                            Intent intent = new Intent();
                            intent.setClass(context, HomeThingComplainActivity.class);
                            context.startActivity(intent);
                        }
                    } else {
                        Intent intent = new Intent();
                        intent.setClass(context, PersonalLoginActivity.class);
                        ((MainActivity) context).startActivityForResult(intent, 101);
                    }

                } else if ("gjfw".equals(menuList.get(position).getFuc_classname())) {
                    if (spn.getBoolean(SPN_IS_LOGIN, false)) {
                        if (spn.getString(SPN_SQID, "").equals("")) {
                            CustomToast.showToast(context, "请选择小区", 1000);
                        } else {
                            Intent intent = new Intent();
                            intent.setClass(context, HomeAuthAddressListActivity.class);
                            context.startActivity(intent);
                        }
                    } else {
                        Intent intent = new Intent();
                        intent.setClass(context, PersonalLoginActivity.class);
                        ((MainActivity) context).startActivityForResult(intent, 101);
                    }
                } else if ("jrfw".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomeJrfwActivity.class);
                    context.startActivity(intent);
                } else if ("jtyl".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomeYlActivity.class);
                    context.startActivity(intent);
                } else if ("swfw".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, CommunitySwInfoActivity.class);
                    context.startActivity(intent);
                } else if ("smfw".equals(menuList.get(position).getFuc_classname())) {
                    ((MainActivity) context).viewPager.setCurrentItem(1);
                } else if ("shjf".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomeLifeJfActivity.class);
                    context.startActivity(intent);
                } else if ("wyfw".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomePropertyServiceActivity.class);
                    context.startActivity(intent);
                } else if ("ylfw".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomeYlFwActivity.class);
                    context.startActivity(intent);
                } else if ("dcjs".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomeLandListActivity.class);
                    context.startActivity(intent);
                } else if ("jkda".equals(menuList.get(position).getFuc_classname())) {
                    if (spn.getBoolean(SPN_IS_LOGIN, false)) {
                        Intent intentscdd = new Intent(context, PersonalHealthActivity.class);
                        intentscdd.putExtra("tag", "2");
                        context.startActivity(intentscdd);
                    } else {
                        Intent intent = new Intent();
                        intent.setClass(context, PersonalLoginActivity.class);
                        ((MainActivity) context).startActivityForResult(intent, 101);
                    }
                } else if ("yygh".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomeLifeDetailActivity.class);
                    intent.putExtra("tag", "lc");
                    context.startActivity(intent);
                } else if ("bgcx".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomeLifeDetailActivity.class);
                    intent.putExtra("tag", "lc");
                    context.startActivity(intent);
                } else if ("zls".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomeLifeDetailActivity.class);
                    intent.putExtra("tag", "sf");
                    context.startActivity(intent);
                } else if ("lcbx".equals(menuList.get(position).getFuc_classname())) {
                    Intent intent = new Intent();
                    intent.setClass(context, HomeSearchProjectActivity.class);
                    intent.putExtra("func_id", menuList.get(position).getId());
                    intent.putExtra("name", menuList.get(position).getFun_name());
                    context.startActivity(intent);
                }

            } else if ("2".equals(menuList.get(position).getFuncType())) {
                Intent intent = new Intent();
                intent.setClass(context, HomeSearchProjectActivity.class);
                intent.putExtra("func_id", menuList.get(position).getFun_cid());
                intent.putExtra("name", menuList.get(position).getFun_name());
                context.startActivity(intent);

            } else if ("4".equals(menuList.get(position).getFuncType())) {
                Intent intent = new Intent(context, CommunitySupermarketListActivity.class);
                intent.putExtra("func_id", menuList.get(position).getFun_cid());
                intent.putExtra("type", "sm");
                intent.putExtra("name", menuList.get(position).getFun_name());
                context.startActivity(intent);
            }
        }
    }*/

    /*private void httpGetKeFu(String url) {

        JsonObjectHeadersPostRequest request = new JsonObjectHeadersPostRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String recode, msg;
                try {
                    if (response.has("recode")) {
                        recode = response.getString("recode");
                        msg = response.getString("msg");
                        if (recode.equals("0")) {
                            if (response.has("phone")) {
                                phone = response.getString("phone");
                            }
                            handler.sendEmptyMessage(1);
                        } else if (recode.equals("111111")) {
                            CancelPublicActivity.getInstance().deleteData(context, msg);
                        } else {
                            CustomToast.showToast(context, msg, 1000);
                        }
                    }
                } catch (Exception e) {
                    CustomToast.showToast(context, R.string.http_fail, 1000);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                CustomToast.showToast(context, R.string.http_fail, 1000);
            }
        });
        queue.add(request);
    }*/

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    //NormalDialogStyle();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    /*private void NormalDialogStyle() {

        final NormalDialog dialog = new NormalDialog(context);
        dialog.content("确定拨打" + phone + "?")
                .style(NormalDialog.STYLE_TWO)
                .titleTextSize(18)
                .showAnim(bas_in)
                .dismissAnim(bas_out)
                .show();

        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        Uri uri = Uri.parse("tel:" + phone);
                        Intent call = new Intent(Intent.ACTION_CALL, uri); //直接播出电话
                        context.startActivity(call);
                        dialog.dismiss();
                    }
                });

    }*/
}
